﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GameFramework.Data;
using GameFramework.DataTable;
using UnityGameFramework.Runtime;
using System;
using Unity.VisualScripting;

namespace TowerDefence_Zombie.Data
{
    public sealed partial class DataPlayer : DataBase
    {
        protected override void OnLoad()
        {
            _playerData = GameEntry.Save.PlayerSaveData;
            if (_playerData.MoneyTypeDataDic.Count == 0)
            {
                InitPlayerData();
            }
            else
            {
                _playerData.OnlineTime += 1;
                //
                DateTime afterTime = DateTime.Parse(_playerData.SignTask[^1]);  
                if(DateTime.Now.Year != afterTime.Year || DateTime.Now.Month!= afterTime.Month || DateTime.Now.Day!= afterTime.Day)
                {
                    _playerData.SignTask.Add(DateTime.Now.ToString());
                }

                //技能
                DateTime nowTime = DateTime.Now;
                DateTime BeOfflineTime = DateTime.Parse(_playerData.PlayerSkillData.BeOfflineTime);
                TimeSpan timeSpan = nowTime - BeOfflineTime;

                //离线收益
                if(timeSpan.Minutes > 0)
                {
                    GameEntry.Data.GetData<MoneyEntity>().GetOffineEarningsData(_playerData.CurLevelIndex,out float offlineEarning,out int OfflineEarningGold);

                    if (_playerData.PlayerSkillData.RemainDoubleAttackTime > 0)
                    {
                        OfflineEarningGold = (int)(1.5f * offlineEarning);
                        var RemainDoubleEarning = (int)(_playerData.PlayerSkillData.RemainDoubleAttackTime / 60);
                        if(RemainDoubleEarning > timeSpan.Minutes)
                            OfflineEarning = timeSpan.Minutes * OfflineEarning * 2;
                        else
                            OfflineEarning = RemainDoubleEarning * OfflineEarning * 2 + (timeSpan.Minutes - RemainDoubleEarning) * OfflineEarning;
                        if(OfflineEarning >=OfflineEarningGold)
                            OfflineEarning = OfflineEarningGold;
                    }
                    else
                    {
                        OfflineEarning = (int)(timeSpan.Minutes * offlineEarning);
                        if (OfflineEarning >= OfflineEarningGold)
                            OfflineEarning = OfflineEarningGold;
                    }
                }

                //技能
                _playerData.PlayerSkillData.RemainDoubleAttackTime -= (float)timeSpan.TotalSeconds;
                if (_playerData.PlayerSkillData.RemainDoubleAttackTime < 0)
                    _playerData.PlayerSkillData.RemainDoubleAttackTime = 0;
                _playerData.PlayerSkillData.RemainDoubleGoldTime -= (float)timeSpan.TotalSeconds;
                if (_playerData.PlayerSkillData.RemainDoubleGoldTime < 0)
                    _playerData.PlayerSkillData.RemainDoubleGoldTime = 0;



            }
            InitDailyTaskEvent();
        }

        public static double GetTotalSeconds(DateTime start, DateTime end)
        {
            TimeSpan timeSpan = end - start;
            return timeSpan.TotalSeconds;
        }
    }
}


